﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseAccess;
using Liguo.AppCode;

namespace Liguo.Store
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //如已登录则清空session，禁止已登录用户注册
            if (Session["User"] != null)
            {
                Session.Clear();
            }
        }

        protected void BtRegister_Click(object sender, EventArgs e)
        {
            //此处将有修改，留给不启用javascript的用户提醒

            #region 用户名检查
            if (TbUserName.Text.Length == 0)
            {
                Response.Write("<script>alert('用户名不能为空！');</script>");
                return;
            }

            if (TbUserName.Text.Length > 20)
            {
                Response.Write("<script>alert('用户名过长！');</script>");
                TbUserName.Text = "";
                return;
            }

            if (!Utilities.IsCJKChar(TbUserName.Text))
            {
                Response.Write("<script>alert('用户名含非法字符！');</script>");
                TbUserName.Text = "";
                return;
            }
            #endregion

            String UserName = TbUserName.Text;

            #region 邮箱检查
            if (TbMail.Text.Length == 0)
            {
                Response.Write("<script>alert('邮箱名不能为空！');</script>");
                return;
            }

            if (TbMail.Text.Length > 60)
            {
                Response.Write("<script>alert('邮箱名过长！');</script>");
                TbMail.Text = "";
                return;
            }

            if (!Utilities.IsANSIChar(TbMail.Text))
            {
                Response.Write("<script>alert('邮箱名含非法字符！');</script>");
                TbMail.Text = "";
                return;
            }
            #endregion

            String MailBox = TbMail.Text;

            #region 密码检查
            if (TbPassword.Text.Length == 0)
            {
                Response.Write("<script>alert('密码不能为空！');</script>");
                return;
            }
            #endregion

            String Password = TbPassword.Text;

            int erro = AddUser(UserName, MailBox, Password);
            if (erro == 0)
            {
                Response.Write("<script>alert('注册成功！');</script>");
                Response.Write("<script>top.location=\"Registed.aspx\";</script>");
            }
            else if (erro == 1)
            {
                Response.Write("<script>alert('用户名已经存在！');</script>");
                TbUserName.Text = "";
                return;
            }
            else if (erro == 2)
            {
                Response.Write("<script>alert('邮箱已经存在！');</script>");
                TbMail.Text = "";
                return;
            }
        }

        private int AddUser(String UserName, String MailBox, String Password)
        {
            SqlIt sql = new SqlIt();
            if (sql.IsExist("UserInfo", "UserName='" + UserName + "'"))
            {
                return 1;
            }
            else if (sql.IsExist("UserInfo", "MailBox='" + MailBox + "'"))
            {
                return 2;
            }
            else
            {
                String Salt = Utilities.GetSalt();
                String MD5Password = Utilities.GetMD5(Password + Salt);
                //状态2为未激活状态
                int Status = 2;
                int Score = 0;
                int Reputation = 0;
                DateTime RegTime = DateTime.Now;
                Dictionary<string, string> info = new Dictionary<string, string>();
                info["UserName"] = UserName;
                info["MailBox"] = MailBox;
                info["Password"] = MD5Password;
                info["Salt"] = Salt;
                info["Status"] = Status.ToString();
                info["Score"] = Score.ToString();
                info["Reputation"] = Reputation.ToString();
                info["RegTime"] = RegTime.ToString("yyy-MM-dd HH:mm:ss");
                sql.AddMess("UserInfo", info);
                //加入session
                int UserID = Convert.ToInt32(sql.SingleSpecialItem("UserInfo", "UserName='" + UserName + "'", "ID"));
                Session.Add("UserID", UserID);
                Session.Add("UserName", UserName);
                Session.Add("MailBox", MailBox);
                Session.Timeout = 30;
                return 0;
            }
        }
    }
}